﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
namespace WPFExcelReport
{
    //完成移库功能，从CP移入CW，同时进行包装
    /// <summary>
    /// 从CP移入CW，但是不进行WV03
    /// </summary>
    public partial class TDTWB06
    {
        #region ==FreeWBB===========
        public static readonly DependencyProperty FreeWBBProperty = DependencyProperty.Register("FreeWBB",
            typeof(double), typeof(TDTWB06),
            new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnFreeWBBChanged));
        public double FreeWBB
        {
            get
            {
                return (double)GetValue(FreeWBBProperty);
            }
            set
            {
                SetValue(FreeWBBProperty, value);
            }
        }
        private static void OnFreeWBBChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TDTWB06;
            if (src != null)
            {
                src.FreeWBB = (double)e.NewValue;
            }
        }
        #endregion
        public TDTWB06()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWBBST();
            this.DataContext = MV;
            InitializeComponent();
        } 
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e); 
        } 
       
        public void WB2WH(MessageInfo msg)
        {
            TargetMTR = 0;
            if (msg == null)
                return;
            if (LIEFSCH.Length < 1)
            {
                TBMain.Clear();
                return;
            }
            msg.LO = SLO;
            msg.LB = SLB;
            DataSet SResult;
            SResult = TMV.WB2WH(msg);
            MergeData(SResult, false);
            cvMaster.CustomFilter = "WBBLO='" + SLO + "' AND WBBLB='" + SLB + "'";
        }
         
      
        public override void OnEXAUFChg(string vlaue)
        {
            TBMain.Clear();
            if (EXAUF.Length < 20)
                return;
            LIEFSCH = EXAUF.Substring(11, 8);
            PM["AUFTYP"] = EXAUF.Substring(0, 1);
            PM["KRS"] = EXAUF.Substring(1, 2);
            PM["LFD"] = EXAUF.Substring(3, 6);
            PM["POS"] = EXAUF.Substring(9, 3);
            PM["LIEFSCH"] = EXAUF.Substring(12, 8);
            PM["LO"] = SLO;
            PM["LB"] = SLB;
            SResult = TMV.WB2WH(PM);
            MergeData(SResult, true, true);
            base.OnEXAUFChg(vlaue);
        }

        private void dlgMaster_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (dwMaster == null)
                return;
            if (PACKNR == "")
            {
                MessageBox.Show("请输入箱子号！");
                return;
            }
            TMV.WBBPACKNR = PACKNR;
            vwMaster.View.MoveCurrentToNext();
        }

        private void dlgMaster_MouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            TMV.WBBPACKNR = "0000";
        }
        public override void OnSave()
        {
            if (!IsSaved)
                return;
            foreach(DataRow row in TBMain.Rows)
            {
                if (row["WBBPACKNR"].ToString() == "0000")
                    continue;
                    TMV.UWB23(row);
            }
            base.OnSave();
        }

        public override void OnAutoChg(bool value)
        {
            if (!IsAuto)
                return;
            filter = "WBBPACKNR='0000'";
            rows = TBMain.Select(filter);
            FreeWBB = rows.Length; 
            vwMaster.View.MoveCurrentToFirst();
            base.OnAutoChg(value);
        }
        public override void OnPACKNRChg()
        {
            if (!IsAuto)
                return; 
            while (true)
            {
                filter = "WBBPACKNR='0000'";
                rows = TBMain.Select(filter); 
                if (rows.Length == 0)
                { 
                    break;
                }
                if (TMV.WBBPACKNR == "0000")
                {
                    TMV.WBBPACKNR = PACKNR;
                    break;
                }
                vwMaster.View.MoveCurrentToNext();
            }           
            base.OnPACKNRChg();
        }
        public override void OnSTKNRChg(string value)
        {
            if (value.Length == 0)
                return;
            
            base.OnSTKNRChg(value);
        }
    }
}
